package com.ossjk.config.shiro;
//package com.example.shirojwt.shiro;


import javax.servlet.http.HttpServletRequest;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ossjk.core.constant.Constant;
import com.ossjk.core.util.CommonUtil;
import com.ossjk.core.util.JwtTokenUtil;
import me.chanjar.weixin.common.redis.RedisTemplateWxRedisOps;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;

import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import java.util.Collection;
import java.util.Map;


/**
 * @author chair
 */

@Component
public class MyRealm extends AuthorizingRealm {

    @Autowired
    private JwtTokenUtil jwtTokenUtil;
    @Autowired
    private RedisTemplate<String, String> redisTemplate;


    /**
     * 必须重写此方法，不然Shiro会报错
     *
     * @param token
     * @return
     */
    @Override
    public boolean supports(AuthenticationToken token) {
        return token instanceof JwtToken;
    }

    /**
     * 默认使用此方法进行用户名正确与否验证，错误抛出异常即可。
     *
     * @param token
     * @return
     * @throws AuthenticationException
     */
    @Override
    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken token) throws AuthenticationException {
        JwtToken jwtToken = (JwtToken) token;
        String tokenStr = (String) jwtToken.getCredentials();
        // 校验有没有token异常
        if (CommonUtil.isBlank(tokenStr)) {
            throw new NoTokenException("没有token异常");
        }
        // 校验token是否失效
        String uid = jwtTokenUtil.getUserId(tokenStr);
        if (CommonUtil.isBlank(uid)) {
            throw new AuthenticationException("token失效");
        }

        //查询redis
        String tokenStr2 = (String) redisTemplate.boundValueOps(Constant.REQUEST_HEADER_TOKEN + uid).get();
        if (CommonUtil.isBlank(tokenStr2)) {
            throw new AuthenticationException("非法token");
        }

        if (!CommonUtil.isEquals(tokenStr, tokenStr2)) {
            throw new AuthenticationException("可能在别的地方登录");
        }
        if (jwtTokenUtil.isFlush(tokenStr)) {
            throw new TokenFlushException("token刷新");
        }
        //token通过
        return new SimpleAuthenticationInfo(tokenStr, tokenStr, getName());
    }

    /**
     * 只有当需要检测用户权限的时候才会调用此方法，例如checkRole,checkPermission之类的
     *
     * @param principals
     * @return
     */
    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        return authorizationInfo;
    }

}
